package hbase;

/**
 * @author cheng
 * @date 2022-03-10 23:05
 */
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HbaseUtil {

     static final String c="info";

    //    static{
//        conf = HBaseConfiguration.create();
//    }
    // 放置初始化方法
//    public void initConfig() {
//        conf = HBaseConfiguration.create();// 配置
//        conf.set("hbase.zookeeper.quorum", "192.168.24.128");// zookeeper地址
//        conf.set("hbase.zookeeper.property.clientPort", "2181");// zookeeper端口
//        conf.setInt("hbase.rpc.timeout", 20000);
//        conf.setInt("hbase.client.operation.timeout", 20000);
//        conf.setInt("hbase.client.scanner.timeout.period", 20000);
//        conf.set("hbase.client.ipc.pool.type", "RoundRobinPool");
//    }

    public  String  insertinfo(String ip,String port,String tableName,List<Book> list) throws Exception{
        Connection con=getConnection(ip,port);
        HBaseAdmin admin = (HBaseAdmin)con.getAdmin();
        Table table = con.getTable(TableName.valueOf(tableName));
        boolean b = admin.tableExists(TableName.valueOf(tableName));
        if(!b){
            createTable(admin,tableName);
        }
        insertList(table,list);
        return "添加成功";
    }
    //创建表的方法
    private  void createTable(Admin admin, String tableName) throws Exception {
        HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
        HColumnDescriptor descriptor2 = new HColumnDescriptor("info");
        descriptor.addFamily(descriptor2);
        admin.createTable(descriptor);
    }
    //插入数据的方法
    private  void insertList(Table table, List<Book> list) throws Exception {
        for (Book book : list) {
            Put put = new Put(book.getBooksId().getBytes());
            put.addColumn(c.getBytes(), "name".getBytes(), book.getName().getBytes());
            put.addColumn(c.getBytes(), "amount".getBytes(), book.getAmount().getBytes());
//            put.addColumn(c.getBytes(), "type".getBytes(), book.getType().getBytes());
//            put.addColumn(c.getBytes(), "bookStatus".getBytes(), book.getBookStatus().getBytes());
            table.put(put);
        }
    }
    //获得与hbase的连接
    private static Connection getConnection(String ip, String port) throws Exception {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", ip);
        configuration.set("hbase.zookeeper.property.clientPort", port);
        Connection connection = ConnectionFactory.createConnection(configuration);
        return connection;
    }

//    public  void main(String[] args) {
//        ArrayList<Book> bookArrayList = new ArrayList<>();
//        Book book = new Book();
//        book.setBooksId("0001");
//
//        book.setName("omk");
//        bookArrayList.add(book);
//        try {
//            insertinfo("niit01","2181","Book",bookArrayList);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//
//    }

}
